<template>
  <div class="login">
    <el-row>
      <el-col :span="24">
        <div class="header">
          <el-image style="width: 48px" :src="logoNoBg"></el-image>
          <h1>exam考试系统</h1>
        </div>
        <div class="container">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            size="large"
          >
            <!-- 账户 -->
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                type="text"
                auto-complete="off"
                placeholder="账号"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="user"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-form-item>
            <!-- 密码 -->
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off"
                placeholder="输入密码"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="password"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-form-item>

            <!--  验证码 -->
            <el-form-item class="login-code" prop="code" v-if="captchaEnabled">
              <el-row :gutter="10">
                <el-col :span="16">
                  <el-input
                    class="code-input"
                    v-model="loginForm.code"
                    auto-complete="off"
                    placeholder="输入验证码"
                    @keyup.enter.native="handleLogin"
                  >
                    <svg-icon
                      slot="prefix"
                      icon-class="validCode"
                      class="el-input__icon input-icon"
                    />
                  </el-input>
                </el-col>
                <el-col :span="5">
                  <div class="code-img">
                    <img
                      :src="codeUrl"
                      @click="getCode"
                      class="login-code-img"
                    />
                  </div>
                </el-col>
              </el-row>
            </el-form-item>
            <el-form-item>
              <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
            </el-form-item>
            <el-form-item>
              <el-button
                :loading="loading"
                type="primary"
                style="width: 100%; border-radius: 12px"
                v-if="!loading"
                @click.native.prevent="handleLogin"
                >登 录
              </el-button>
              <el-button
                type="primary"
                style="width: 100%; border-radius: 12px"
                v-else
                >登 录 中...
              </el-button>
              <div style="width: 100%; border-radius: 12px" v-if="register">
                <router-link class="link-type" :to="'/register'"
                  >立即注册</router-link
                >
              </div>
            </el-form-item>
          </el-form>
          <el-image :src="welcome"></el-image>
        </div>
        <!--  底部  -->
        <div class="footer">
          <el-link
            style="color: #ccc"
            href="https://https://beian.miit.gov.cn/"
            target="_blank"
            >Copyright © 2018-2022 All Rights
            Reserved.蜀ICP备19035437号</el-link
          >
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import welcome from "@/assets/images/login-welcome.png";
import logo from "@/assets/logo/logo.png";
import logoNoBg from "@/assets/logo/logo-noBg.png";

export default {
  name: "Login",
  data() {
    return {
      logo,
      logoNoBg,
      welcome,
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  width: 100%;
  height: 100%;
  // fix：F12弹起页面时，背景颜色不全
  min-height: 800px;
  // background-color: #0099cc; #99CCFF;
  // background-color: #87ceeb;
  background-color: #0099cc;
  text-align: center;
  color: #333333;
  .header {
    height: 50px;
    font-size: 18px;
    margin: 40px 0 20px 0;
    display: flex;
    justify-content: center;
    align-items: center;
    letter-spacing: 5px;

    h1 {
      margin-left: 20px;
      color: white;
      background-clip: text;
      -webkit-background-clip: text;
    }
  }
  .container {
    box-sizing: content-box;
    padding: 10px 40px 5px 40px;
    width: 880px;
    margin: 80px auto 60px auto;
    // background-color: #f0f2f5;
    background-color: #f0f2f5b8;
    backdrop-filter: blur(20px);
    border-radius: 20px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    .el-form {
      text-align: left;
      width: 500px;
      .el-form {
        .el-input {
          border-radius: 20px;
        }
      }
      .el-form-item.login-code {
        display: flex;
        flex-direction: row;
        align-items: center;
        .code-img {
          width: 37%;
          img {
            width: 140px;
            height: 38px;
            cursor: pointer;
            vertical-align: middle;
          }
        }
      }
    }
    img {
      width: 530px;
      height: 450px;
    }
  }
  .footer {
    margin-top: 20px;
    letter-spacing: 2px;
    // color: white;
  }
}
</style>
 
